package com.ag.hmk.action;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ag.bean.TbAidgoodsku;
import com.ag.bean.TbAidgoogs;
import com.ag.bean.TbGoods;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.PageUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
/**
 * 
 * @author hmk
 * 查询物资库列表
 */
public class FindSomeAGKUAction extends BaseAction {
	private static final long serialVersionUID = -4270657116058210604L;
	
	private HmkUiltServImpl hmkUiltServImpl;
	private int page;
	private int maxRow;
	private String wukuType;
	private String agkuName;
	private String agkuLocaton;
	
	private PageUtil pageUtil; //返回结果
	
	private List<String> goodsList; //返回库对应的物品串
	
	/*
	 *查询物资库列表(分页查询) 
	 */
	public String findAGKUList()throws Exception{
		PageUtil pUtil = new PageUtil();
		List result=null;
		List<String> strList = new ArrayList<String>();
		int total=0;
		try {
			String cql = "select count(*) from TbAidgoodsku agku where 1=1";
			String hql = "from TbAidgoodsku agku  left join fetch agku.tbType left join fetch agku.tbAidgoogses ag " +
			"left join fetch ag.tbGoodsstatus left join fetch ag.tbGoods where 1=1";
			System.out.println("----"+wukuType);
			if (wukuType!=null && wukuType.length()>0) {
				cql += " and agku.tbType.tpyeId = " + wukuType;
				hql += " and agku.tbType.tpyeId = " + wukuType;
			}
			System.out.println("----"+agkuName);
			if (agkuName!=null && agkuName.length()>0) {
				cql += " and agku.aidName like '%" + agkuName+"%'";
				hql += " and agku.aidName like '%" + agkuName+"%'";
			}
			System.out.println("----"+agkuLocaton);
			if (agkuLocaton!=null && agkuLocaton.length()>0) {
				cql += " and agku.aidLocation like '%" + agkuLocaton+"%'";
				hql += " and agku.aidLocation like '%" + agkuLocaton+"%'";
			}
			cql+=" and agku.a='"+IDUtil.PRESERVE+"' ";
			hql+=" and agku.a='"+IDUtil.PRESERVE+"'  order by agku.aidId desc";
			//获取条数
			total = hmkUiltServImpl.getCount(cql);
			if (page == 0) {
				page = 1;
			}
			if (maxRow == 0) {
				maxRow = 10;
			}
			int firstRow = (page - 1) * maxRow;
			//处理库中的物资开始
			result = hmkUiltServImpl.query(hql, firstRow, maxRow);
			if (result!=null) {
				Iterator it1 = result.iterator();
					while(it1.hasNext()){
						String str1 = "";
						int i = 0;
						TbAidgoodsku ku1 = (TbAidgoodsku)it1.next();
						Set<TbAidgoogs> agSet = ku1.getTbAidgoogses();
						Iterator<TbAidgoogs> it2 = agSet.iterator();
						while(it2.hasNext()){
							TbAidgoogs ag2 =(TbAidgoogs)it2.next();
							TbGoods g2 = (TbGoods) ag2.getTbGoods();
							str1 +=(g2.getGoodsName()+",");
							System.out.println("===="+ag2.getAgNumber());
							i++;
							if (i>4) {
								break;
							}
						}
						str1 +="...";
						strList.add(str1);
					}
				}	
			//处理库中的物资结束
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
			strList = null;
			total = 0;
		}
		this.setGoodsList(strList); //设置物品串
		pUtil.setTotal(total);
		pUtil.setPage(page);
		pUtil.setMaxRow(maxRow);
		pUtil.setList(result);
		
		this.setPageUtil(pUtil);
		return "findAGKUListOK";
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}



	public void setWukuType(String wukuType) {
		this.wukuType = wukuType;
	}



	public PageUtil getPageUtil() {
		return pageUtil;
	}


	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}


	public void setPage(int page) {
		this.page = page;
	}


	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public List<String> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<String> goodsList) {
		this.goodsList = goodsList;
	}

	public void setAgkuName(String agkuName) {
		this.agkuName = agkuName;
	}

	public void setAgkuLocaton(String agkuLocaton) {
		this.agkuLocaton = agkuLocaton;
	}

	
	

}
